package cn.loioi.util.page.datatable;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import cn.loioi.util.page.datatable.request.DataTableColumn;
import cn.loioi.util.page.datatable.request.DataTableOrder;
import cn.loioi.util.page.datatable.request.DataTableOrderDir;
import cn.loioi.util.page.datatable.request.DataTableSearch;


public class DataTableRequest {
	private Map<String, DataTableColumn> columns = new HashMap<>();
	private Map<String, DataTableOrder> order = new HashMap<>();
	private DataTableSearch search;
	private Integer start = 1;
	private Integer length = 10;
	private Integer draw = 1;

	public static DataTableRequest pasre(Map<String, String[]> parameterMap) {
		DataTableRequest request = new DataTableRequest();
		for (Entry<String, String[]> entry : parameterMap.entrySet()) {
			if ("start".equals(entry.getKey())) {
				request.setStart(DataTableUitl.parseInt(StringUtils.join(entry.getValue())));
			} else if ("length".equals(entry.getKey())) {
				request.setLength(DataTableUitl.parseInt(StringUtils.join(entry.getValue())));
			} else if ("draw".equals(entry.getKey())) {
				request.setDraw(DataTableUitl.parseInt(StringUtils.join(entry.getValue())));
			} else if (entry.getKey().startsWith("columns")) {
				String[] key = entry.getKey().split("\\]\\[|\\[|\\]");
				request.getColumn(key[1]).setValue(key, StringUtils.join(entry.getValue()));
			} else if (entry.getKey().startsWith("order")) {
				String[] key = entry.getKey().split("\\]\\[|\\[|\\]");
				DataTableOrder o = request.getOrder(key[1]);
				if ("dir".equals(key[2])) {
					o.setDir(DataTableOrderDir.pasre(StringUtils.join(entry.getValue())));
				} else if ("column".equals(key[2])) {
					o.setColumn(request.getColumn(StringUtils.join(entry.getValue())));
				}
			} else if (entry.getKey().startsWith("search")) {
				if ("search[value]".equals(entry.getKey())) {
					request.getSearch().setValue(StringUtils.join(entry.getValue()));
				} else if ("search[regex]".equals(entry.getKey())) {
					request.getSearch().setRegex(DataTableUitl.parseBoolean(StringUtils.join(entry.getValue())));
				}
			}
		}
		return request;
	}

	public Integer getStart() {
		return start;
	}

	public void setStart(Integer start) {
		this.start = start;
	}

	public Integer getPageNum() {
		return (start / length) + 1;
	}

	public Integer getLength() {
		return length;
	}

	public void setLength(Integer length) {
		this.length = length;
	}

	public Integer getDraw() {
		return draw;
	}

	public void setDraw(Integer draw) {
		this.draw = draw;
	}

	public Map<String, DataTableColumn> getColumns() {
		return columns;
	}

	public DataTableColumn getColumn(String index) {
		if (this.columns.get(index) == null) {
			this.columns.put(index, new DataTableColumn());
		}
		return this.columns.get(index);
	}

	public void setColumns(Map<String, DataTableColumn> columns) {
		this.columns = columns;
	}

	public Map<String, DataTableOrder> getOrder() {
		return order;
	}

	public DataTableOrder getOrder(String index) {
		if (this.order.get(index) == null) {
			this.order.put(index, new DataTableOrder());
		}
		return this.order.get(index);
	}

	public void setOrder(Map<String, DataTableOrder> order) {
		this.order = order;
	}

	public DataTableSearch getSearch() {
		if (search == null) search = new DataTableSearch();
		return search;
	}

	public void setSearch(DataTableSearch search) {
		this.search = search;
	}

}
